home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Utilities / Biomorph 0.77 / Biomorph src / savepict.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-05  |  2.4 KB  |  119 lines  |  [TEXT/ALFA]

  1. /*****************
  2. ** savepict.c
  3. **
  4. ** routines to handle saving the image
  5. ** generated as a pict file.
  6. ******************/
  7.  
  8. #include <MacHeaders>
  9. #include "globals.h"
  10. #include "constants.h"
  11.  
  12. static int Write512b( int refNum);
  13.  
  14. /*****************
  15. ** SavePict()
  16. **
  17. ** This routine saves the generated image as a PICT file.
  18. ******************/
  19. void SavePict(void)
  20. {
  21.     PicHandle    thePict;
  22.     GrafPtr        savedPort;
  23.     Point        where= {100,100};
  24.     Str255        prompt, name;
  25.     SFReply        reply;
  26.     int            fRefNum;
  27.     long int    size;
  28.     int            result;
  29.     
  30.     // get file info to save document as
  31.     GetIndString( prompt, kErrStrList, kSavePrompt);
  32.     GetIndString( name,   kErrStrList, kUntitledName);
  33.     SFPutFile( where, prompt, name, NULL, &reply);
  34.     
  35.     if (!reply.good)    // if user cancels it, return.
  36.         return;
  37.     
  38.     // open file, setting type/creator to 'PICT' and 'MORF'
  39.     result = Create( reply.fName, reply.vRefNum,
  40.                     kFileCreator, kPictFileType);
  41.                     
  42.     switch (result) {
  43.     case noErr:
  44.         // file was created OK -- open it
  45.         if (FSOpen( reply.fName, reply.vRefNum, &fRefNum) != noErr)
  46.         {
  47.             Error(kFileOpenErr, 0, 0, 0, noteIcon);
  48.             return;
  49.         }
  50.         break;
  51.  
  52.     case dupFNErr:
  53.         // file already existed.  Overwrite it.
  54.         if (FSOpen( reply.fName, reply.vRefNum, &fRefNum) != noErr)
  55.         {
  56.             Error(kFileOpenErr, 0, 0, 0, noteIcon);
  57.             return;
  58.         }
  59.         if (SetEOF( fRefNum, 0L) != noErr)
  60.         {
  61.             Error(kFileOpenErr, 0, 0, 0, noteIcon);
  62.             FSClose( fRefNum);
  63.             return;
  64.         }
  65.         break;
  66.     default:
  67.         Error(kFileOpenErr, 0, 0, 0, noteIcon);
  68.         return;
  69.         break;
  70.     } // switch
  71.  
  72.     
  73.     // write out the initial 512 bytes of stuff.  Nothing important.
  74.     if (Write512b( fRefNum) != noErr)
  75.     {
  76.         Error( kFileSaveErr, 0, 0, 0, cautionIcon);
  77.         FSClose( fRefNum);
  78.         return;
  79.     }
  80.     
  81.     GetPort( &savedPort);
  82.     SetPort( gMainWindow);
  83.     ClipRect( &gMainWindow->portRect);
  84.     
  85.     thePict = OpenPicture( &gMainWindow->portRect);
  86.     
  87.     CopyBits( &gOffBM, &gMainWindow->portBits,
  88.               &gOffGP.portRect, &gMainWindow->portRect, srcCopy, NULL);
  89.     
  90.     ClosePicture();
  91.     
  92.     // write out the data at **thePict
  93.  
  94.     size = GetHandleSize( (Handle)thePict);
  95.     if ( FSWrite( fRefNum, &size, *thePict) != noErr)
  96.         Error(kFileSaveErr, 0, 0, 0, cautionIcon);
  97.     
  98.     FSClose( fRefNum);
  99.     
  100.     KillPicture( thePict);
  101. } // SavePict()
  102.  
  103.  
  104. static int Write512b( int refNum)
  105. {
  106.     Ptr p;
  107.     int code;
  108.     long int size=512;
  109.     
  110.     if ( (p = NewPtrClear( size)) == NULL)
  111.         return !noErr;    // write failed
  112.     code = FSWrite( refNum, &size, p);
  113.     if (code==noErr && size!=512)
  114.         code = !noErr;
  115.     DisposPtr(p);
  116.     return code;
  117. } // Write512b
  118.  
  119.